package com.example.mountain;

import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;

public class Mos extends Fragment implements View.OnClickListener {
	Camera camera = null;
	Context context = null;
	boolean isOn = false;
	Button MosBtn = null;
	Parameters params;

	String S = Parameters.FLASH_MODE_TORCH;
	String o = Parameters.FLASH_MODE_OFF;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_mos, viewGroup,
				false);

		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MosBtn = (Button) getActivity().findViewById(R.id.mos_btn);

		MosBtn.setOnClickListener(this);
		if (!isSupportedFlash()) {
			Toast.makeText(getActivity(), "Flash 미지원", Toast.LENGTH_SHORT)
					.show();
		} else {
			cameraRelease();
			try {
				camera = Camera.open();
			} catch (RuntimeException e) {
				camera = null;
				Toast.makeText(getActivity(), "CAMERA 자원에 접근불가",
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	public void onDestroyView() {
		cameraRelease();
		super.onDestroyView();
	}

	void cameraRelease() {
		if (camera != null) {
			camera.release();
			camera = null;
		}
	}

	boolean isSupportedFlash() {
		PackageManager pm = getActivity().getPackageManager();
		return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)
				&& pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
	}

	@Override
	public void onClick(View view) {

		if (camera == null) {
			Toast.makeText(getActivity(), "Flash 미지원", Toast.LENGTH_SHORT)
					.show();
		} else {
			isOn = !isOn;
			params = camera.getParameters();

			if (isOn) {

				try {
					params.setFlashMode(S);
					MosBtn.setText("ON");
					// thread.start();

					handler.sendEmptyMessageDelayed(0, 3000);
					handler.removeMessages(0);

					if (isOn == false) {
						params.setFlashMode(S);
						MosBtn.setText("ON");

					}

				} catch (Exception e) {
					e.printStackTrace();
				}

			} else {
				params.setFlashMode(Parameters.FLASH_MODE_OFF);
				MosBtn.setText("OFF");
			}
			camera.setParameters(params);
			camera.startPreview();
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {

				if (Parameters.FLASH_MODE_TORCH != null) {

					params.setFlashMode(Parameters.FLASH_MODE_OFF);
					MosBtn.setText("OFF");
					camera.setParameters(params);
					camera.startPreview();

					handler.sendEmptyMessageDelayed(1, 3000);
					handler.removeMessages(1);

				}

			}
			if (msg.what == 1) {
				isOn = !isOn;
				params = camera.getParameters();

				params.setFlashMode(Parameters.FLASH_MODE_TORCH);
				MosBtn.setText("ON");
			}

		}
	};


}
